package pe.com.bbva.configuracion.dao.impl;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pe.com.bbva.configuracion.dao.TurnoDAO;
import pe.com.bbva.configuracion.domain.Turno;
import pe.com.bbva.util.Constantes;
import pe.com.stefanini.core.dao.GenericDAOImpl;
import pe.com.stefanini.core.exceptions.BOException;
import pe.com.stefanini.core.exceptions.DAOException;
import pe.com.stefanini.core.util.StringUtil;

@Service("turnoDAO")
public class TurnoDAOImpl extends GenericDAOImpl<Turno> implements TurnoDAO{
	Logger logger = Logger.getLogger(this.getClass());
	@Autowired
	public TurnoDAOImpl(SessionFactory sessionFactory) {
		super(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	public List<Turno> findTurnos(Turno turno)throws BOException, DAOException {
		String where = "";
		if(turno.getDescripcion()!= null && !turno.getDescripcion().equals("")){
			where = where + (where==""?"":" AND ") + "upper(descripcion) like upper('%" +	turno.getDescripcion() + "%') ";
		}
		if(turno.getHoraInicio()!= null && !turno.getHoraInicio().equals("")){
			where = where + (where==""?"":" AND ") + "horaInicio like '%" +	turno.getHoraInicio() + "%'";
		}
		if(turno.getHoraFin()!= null && !turno.getHoraFin().equals("")){
			where = where + (where==""?"":" AND ") + "horaFin like '%" + turno.getHoraFin() + "%'";
		}
		if(turno.getEstadoEjecucion()!= null && !turno.getEstadoEjecucion().equals("")){
			where = where + (where==""?"":" AND ") + "estadoEjecucion = '" + turno.getEstadoEjecucion() + "'";
		}
		if(turno.getEstado()!= null && !turno.getEstado().equals("")){
			where = where + (where==""?"":" AND ") + "estado = '" + turno.getEstado() + "'";
		}
		if(turno.getId()!= null && !turno.getId().equals("")){
			where = where + (where==""?"":" AND ") + "id = '" +	turno.getId() + "'";
		}
		if(!where.equals("")){
			where = Constantes.WHERE_CLAUSE + where;
		}
		String orders = " order by id, fechaCreacion";
		List<Turno> listaTurnos = super.executeQuery(Turno.class,where,orders);
		return listaTurnos;
	}
	
	@SuppressWarnings("unchecked")
	public Turno findActive() throws BOException, DAOException{
		Turno turno = null;
		String where = Constantes.WHERE_CLAUSE +" estadoEjecucion <> '0'";
		String orders = " order by fechaCreacion desc";
		List<Turno> listaTurnos = super.executeQuery(Turno.class,where,orders);
		if(listaTurnos!=null && !listaTurnos.isEmpty())
			turno = listaTurnos.get(0);
		return turno;
	}
	
	@SuppressWarnings("deprecation")
	public Long ValidaCargaEnProceso() throws DAOException {
		Session hibernateSession = null;
		Connection con = null;
		CallableStatement cstmt =null;
		ResultSet rs  = null;		
		Long IdCarga=0L;
		try {
			hibernateSession = (Session) getHibernateTemplate().getSessionFactory().openSession();
			con = hibernateSession.connection();
			cstmt =con.prepareCall("{call TARGPS.TARGPSPKG.PR_TTARGPS_VALIDAR_ENPROCESO(?)}");			
			cstmt.registerOutParameter(1,oracle.jdbc.OracleTypes.VARCHAR);		
			cstmt.executeQuery();			
			IdCarga = Long.parseLong((String)cstmt.getObject(1));			
		} catch (HibernateException e) {
			logger.error(StringUtil.getStackTrace(e));
			throw new DAOException(e.getMessage());
		} catch (SQLException e) {
			logger.error(StringUtil.getStackTrace(e));
			throw new DAOException(e.getMessage());
		} catch (Exception e) {
			logger.error(StringUtil.getStackTrace(e));
			throw new DAOException(e.getMessage());
		}finally{
			try {
				if(rs!=null){
					rs.close();
				}
				if(cstmt!=null){
					cstmt.close();
				}
				hibernateSession.close();
			} catch (SQLException e) {
				throw new DAOException(e.getMessage());
			} catch (HibernateException e) {
				logger.error(StringUtil.getStackTrace(e));
				throw new DAOException(e.getMessage());
			} 
		}
		return IdCarga;
	}
}